package itsm.isperp.module.controller.api.wechat;

import itsm.isperp.framework.core.context.ContextHolder;
import itsm.isperp.framework.web.paging.RenderPager;
import itsm.isperp.framework.web.request.JqGridFilterRuleOp;
import itsm.isperp.framework.web.request.JqGridRequest;
import itsm.isperp.module.dto.solr.SolrKngKnowledgeDTO;
import itsm.isperp.module.entity.app.AppUser;
import itsm.isperp.module.service.app.AppUserService;
import itsm.isperp.module.service.knowledge.KngKnowledgeCommentService;
import itsm.isperp.module.service.knowledge.KngKnowledgeItemService;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.solr.core.query.Criteria;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2014-3-15
 * @since 1.0
 * 
 */
@Controller
@RequestMapping("api/wechat/knowledge")
public class WechatKnowledgeController {

  @Autowired
  KngKnowledgeItemService kngKnowledgeItemService;

  @RequestMapping("search")
  public ModelAndView search(String keyword, String page, String openid) {

    ModelAndView mv = new ModelAndView("api/wechat/knowledge/search");
    int pageNo = StringUtils.isEmpty(page) ? 0 : Integer.parseInt(page.trim());

    mv.addObject("keyword", keyword);

    Pageable pg = new PageRequest(pageNo, 5);

    if (StringUtils.isEmpty(keyword)) {
      JqGridRequest request = new JqGridRequest(pg);

      if (StringUtils.isNotBlank(openid)) {
        AppUserService appUserService = ContextHolder
            .getSpringBean(AppUserService.class);
        AppUser us = appUserService.findByUsername(openid, "are_customer");
        boolean areCustomer = us.isAreCustomer();
        if (areCustomer) {
          request.addFilterRule("access_right", JqGridFilterRuleOp.eq, "public");
        }

      }

      Page<SolrKngKnowledgeDTO> r = this.kngKnowledgeItemService
          .findAllPublishedDetailByRequest(request, 50);

      mv.addObject(
          "pager",
          RenderPager.renderHtml(
              ContextHolder.getWebServerName() + "api/wechat/knowledge/search",
              pageNo,
              5,
              r.getTotalElements(),
              0,
              "nodisplaytext ui-btn ui-shadow ui-corner-all ui-btn-inline ui-mini ui-icon-arrow-l ui-btn-icon-left",
              "nodisplaytext ui-btn ui-shadow ui-corner-all ui-btn-inline ui-mini ui-icon-arrow-r ui-btn-icon-right"));

      mv.addObject("result", r);
      return mv;
    }

    Criteria[] cs = null;

    Page<SolrKngKnowledgeDTO> resultPage = kngKnowledgeItemService
        .searchByKeyword(keyword, pg, cs,null);

    mv.addObject(
        "pager",
        RenderPager.renderHtml(
            "api/wechat/knowledge/search?keyword=" + keyword,
            pageNo - 1,
            5,
            resultPage.getNumberOfElements(),
            0,
            "nodisplaytext ui-btn ui-shadow ui-corner-all ui-btn-inline ui-mini ui-icon-arrow-l ui-btn-icon-left",
            "nodisplaytextc ui-btn ui-shadow ui-corner-all ui-btn-inline ui-mini ui-icon-arrow-r ui-btn-icon-right"));
    mv.addObject("result", resultPage);
    return mv;

  }

  @RequestMapping("detail")
  public ModelAndView view(String id) throws SQLException {

    ModelAndView mv = new ModelAndView("api/wechat/knowledge/detail");
    Map<String, Object> model = kngKnowledgeItemService.findOneDetailById(id);
    mv.addObject("model", model);

    String newContent = model.get("contents").toString();
    newContent = newContent.replace("uploadFile/ueditor",
        "../../../uploadFile/ueditor");
    mv.addObject("newContent", newContent);
    KngKnowledgeCommentService kngKnowledgeCommentService = ContextHolder
        .getSpringBean(KngKnowledgeCommentService.class);

    List<Map<String, Object>> comments = kngKnowledgeCommentService
        .findAllDetailByKnowledgeId(id);
    mv.addObject("comments", comments);
    mv.addObject("commentCount", comments == null ? 0 : comments.size());

    // 阅读数增加1
    // info.setReadTimes(info.getReadTimes() + 1);
    kngKnowledgeItemService.increaseReadTimes(id);
    return mv;
  }
}
